from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from Pages.LoginPage import LoginPage


# 镜像定制页面
class ImageBuild(object):
    def __init__(self, driver):
        self.driver = driver

    # 页面地址
    def get_url(self, url):
        self.driver.get(url)

    # 页面信息检查
    def check_info(self):
        # 流水线列表
        name_title = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/p')
        # print(name_title.text)
        # 添加流水线按钮，用户不登陆时不显示
        pipeline_button = self.driver.find_element(By.XPATH,
                                                   '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div[1]/button')
        if pipeline_button.is_displayed():
            button_info = '添加流水线按钮显示异常'
        else:
            button_info = '用户未登录，添加流水线按钮不显示'

        # 显示输入框
        filter_box = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div['
                                                        '1]/div/div/div[2]')
        if filter_box.is_displayed():
            filter_info = '显示输入框'
        else:
            filter_info = '输入框不显示'

        select_box = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div['
                                                        '1]/div/div/div[1]/div/div/div/div')
        if select_box.is_displayed():
            box_info = '显示筛选下拉框'
        else:
            box_info = '筛选下拉框不显示'

        return [name_title.text, button_info, filter_info, box_info]

    # 用户登录
    def user_login(self, user_name, password):
        login = LoginPage(self.driver)
        login.user_login()
        login.login_valid(user_name, password)

    # 添加流水线按钮
    def add_pipeline(self):
        self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div[1]/button').click()

    # 弹窗取消按钮
    def div_cancel(self):
        self.driver.find_element(By.XPATH,  '//*[@id="app"]/div[1]/div/div[2]/div[2]/div/div/footer/div/button[2]/span').click()

    # 添加流水线弹窗检查
    def pipeline_check(self):
        div = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[2]')
        if div.is_displayed():
            div_info = '添加流水线弹窗显示正常'
        else:
            div_info = '流水线弹窗显示异常'

        return div_info

    # 添加流水线弹窗页面信息检查
    def pipeline_info_check(self):
        info_1 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                                    '1]/label')

        info_2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                                    '2]/label')

        info_3 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                                    '3]/label')

        info_4 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                                    '4]/label')

        info_5 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                                    '5]/label')

        return [info_1.text, info_2.text, info_3.text, info_4.text, info_5.text]

    # 获取页面弹窗
    def alert_message(self):
        wait = WebDriverWait(self.driver, 10)
        alert = wait.until(expected_conditions.alert_is_present())
        # 返回弹窗和弹窗信息
        return alert, alert.text
